package com.chenglai.seckill.service.impl;

import com.chenglai.seckill.dao.SeckillGoodsMapper;
import com.chenglai.seckill.pojo.SeckillGoods;
import com.chenglai.seckill.service.SeckillGoodsService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/23 14:32
 */
@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Resource
    private SeckillGoodsMapper seckillGoodsMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<SeckillGoods> getSeckillGoodsAllList(Integer seckillId) {
        Example example = new Example(SeckillGoods.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("seckillId", seckillId);
        List<SeckillGoods> seckillGoods = seckillGoodsMapper.selectByExample(example);
        seckillGoods.stream().forEach(map -> {
            Integer value = (Integer) redisTemplate.opsForValue().get("seckillGoods:" + map.getGoodsId());
            map.setInventoryNumber(value);
        });
        return seckillGoods;
    }

    @Override
    public Map editSeckillGoods(SeckillGoods seckillGoods) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Map<String, Object> data = new HashMap<>();
        if (seckillGoods.getSgId() == null) {
            //添加
            seckillGoodsMapper.insertSelective(seckillGoods);
            //将库存存入redis中，命名规则：seckill_g_n:活动ID:商品ID,如seckill_g_n:1:258
            redisTemplate.opsForValue().set("seckill_g_n:" + seckillGoods.getSeckillId() + ":" + seckillGoods.getGoodsId(), seckillGoods.getSgNumber());
        } else {
            //TODO 修改 如想添加或减少库存 只能修改sg_number 不能直接修改inventoryNumber，inventoryNumber根据sg_number的变化去修改redis

            SeckillGoods old = seckillGoodsMapper.selectByPrimaryKey(seckillGoods.getSgId());
            Integer oldSgNumber = old.getSgNumber();
            //新值减去旧值得到number的变化
            Integer value = seckillGoods.getSgNumber() - oldSgNumber;
            Integer temp = (Integer) valueOperations.get("seckill_g_n:" + seckillGoods.getSeckillId() + ":" + seckillGoods.getGoodsId());
            Integer newValue = temp + value;
            if (newValue > 0) {
                valueOperations.increment("seckill_g_n:" + seckillGoods.getSeckillId() + ":" + seckillGoods.getGoodsId(),value);
            } else {
                data.put("flag", "error");
                data.put("msg", "库存不能为0");
                return data;
            }
            seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
        }
        data.put("flag", "SUCCESS");
        data.put("msg", "成功");
        return data;
    }

    @Override
    public SeckillGoods detail(Integer sgId) {
        return seckillGoodsMapper.selectByPrimaryKey(sgId);
    }
}
